package com.company.cloud.mall.modules.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.mall.constant.MallErrorEnum;
import com.company.cloud.mall.modules.api.response.OrderRefundsModel;
import com.company.cloud.mall.modules.api.response.OrderReturnsModel;
import com.company.cloud.mall.modules.app.controller.req.OrderAfterSale2UserQuery4PageParam;
import com.company.cloud.mall.modules.app.controller.req.OrderRefundsApplyParam;
import com.company.cloud.mall.modules.app.controller.req.OrderReturnsApplyParam;
import com.company.cloud.mall.modules.app.controller.req.OrderReturnsSendParam;
import com.company.cloud.mall.modules.app.service.ShopAfterSaleRefundsService;
import com.company.cloud.mall.modules.app.service.ShopAfterSaleReturnsService;
import com.company.cloud.mall.modules.base.entity.OrderRefunds;
import com.company.cloud.mall.modules.base.entity.OrderReturns;
import com.company.cloud.mall.modules.base.service.OrderLogService;
import com.company.cloud.mall.modules.base.service.OrderRefundsService;
import com.company.cloud.mall.modules.base.service.OrderReturnsService;
import com.company.cloud.mall.modules.utils.ShopOrderConvertUtils;
import com.company.cloud.user.modules.api.service.UserRemoteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 订单退款
 */
@Login
@RestController
@RequestMapping("/app/mall/orderAfterSale")
@Api(tags="Shop_订单售后")
public class OrderAfterSaleController {

	@Autowired
	private OrderRefundsService orderRefundsService;
	@Autowired
	private OrderLogService orderLogService;
	@Autowired
	private OrderReturnsService orderReturnsService;
	@Autowired
	private UserRemoteService userRemoteService;
	@Autowired
	private ShopAfterSaleRefundsService shopAfterSaleRefundsService;
	@Autowired
	private ShopAfterSaleReturnsService shopAfterSaleReturnsService;
	
	//------------------------------------------------------------------------------------------------
	@GetMapping("/queryOrderAfterByUser")
	@ApiOperation(value="用户根据订单明细查询售后信息", response=BaseResult.class)
	public BaseResult queryOrderAfterByUser(@RequestParam("orderItemId")Integer orderItemId) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<OrderRefunds> refundsList=orderRefundsService.list(new QueryWrapper<OrderRefunds>()
				.eq("user_id", userInfo.getId())
				.eq( "order_item_id", orderItemId)
				.orderByDesc("cdt"));
		List<OrderRefundsModel> refundsData=new ArrayList<OrderRefundsModel>();
		if(!CollectionUtils.isEmpty(refundsList)) {
			refundsList.forEach(entity->{
				refundsData.add(ShopOrderConvertUtils.getOrderRefunds(entity,null));
			});
		}
		List<OrderReturns> returnsList = orderReturnsService.list(
				new QueryWrapper<OrderReturns>()
				.eq("user_id", userInfo.getId())
				.eq( "order_item_id",orderItemId)
				.orderByDesc("cdt"));
		List<OrderReturnsModel> returnsData=new ArrayList<OrderReturnsModel>();
		if(!CollectionUtils.isEmpty(returnsList)) {
			returnsList.forEach(entity->{
				returnsData.add(ShopOrderConvertUtils.getOrderReturns(entity,null));
			});
		}
		Map<String, Object> data=new HashMap<String, Object>();
		data.put("refunds", refundsData);
		data.put("returns", returnsData);
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/queryOrderRefundsByUser")
	@SysLog("用户订单退款单查询")
	@ApiOperation(value="用户订单退款单查询", response=OrderRefundsModel.class)
	public BaseResult queryOrderRefundsByUser(@RequestBody OrderAfterSale2UserQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<OrderRefunds> page = orderRefundsService.page(new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<OrderRefunds>()
				.eq("user_id", userInfo.getId())
				.eq(StringUtils.hasText(params.getOrderSn()), "order_sn", params.getOrderSn())
				.eq(CheckUtils.hasInt(params.getOrderItemId()), "order_item_id", params.getOrderItemId())
				.eq(StringUtils.hasText(params.getState()), "state", params.getState())
				.orderByDesc("cdt")
				);
		System.out.println("================="+page.getRecords());
		IPage<OrderRefundsModel> data = page.convert(new Function<OrderRefunds, OrderRefundsModel>() {
			@Override
			public OrderRefundsModel apply(OrderRefunds entity) {
				return ShopOrderConvertUtils.getOrderRefunds(entity,null);
			}
		});
		System.out.println("================="+data.getRecords());
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/applyOrderRefunds")
	@SysLog("订单退款申请")
	@ApiOperation(value="订单退款申请")
	public BaseResult applyOrderRefunds(@RequestBody OrderRefundsApplyParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		shopAfterSaleRefundsService.add(params, userInfo);
		return BaseResult.ok();
	}
	
	@GetMapping("/getOrderRefunds")
	@SysLog("订单退款明细")
	@ApiOperation(value="订单退款明细")
	public BaseResult getOrderRefunds(@RequestParam("sn")String sn) {
		OrderRefundsModel data=shopAfterSaleRefundsService.get(sn);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/cancelOrderRefunds")
	@SysLog("订单退款撤销")
	@ApiOperation(value="订单退款撤销")
	public BaseResult cancelOrderRefunds(@RequestParam("sn")String sn) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		shopAfterSaleRefundsService.cancel(sn, userInfo);
		return BaseResult.ok();
	}

	
	//------------------------------------------------------------------------------------------------
	@PostMapping("/queryOrderReturnsByUser")
	@ApiOperation(value="用户订单退款退货单查询", response=OrderReturnsModel.class)
	public BaseResult queryOrderReturnsByUser(@RequestBody OrderAfterSale2UserQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<OrderReturns> page = orderReturnsService.page(new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<OrderReturns>()
				.eq("user_id", userInfo.getId())
				.eq(StringUtils.hasText(params.getOrderSn()), "order_sn", params.getOrderSn())
				.eq(CheckUtils.hasInt(params.getOrderItemId()), "order_item_id", params.getOrderItemId())
				.eq(StringUtils.hasText(params.getState()), "state", params.getState())
				.orderByDesc("cdt")
				);
		IPage<OrderReturnsModel> data = page.convert(new Function<OrderReturns, OrderReturnsModel>() {
			@Override
			public OrderReturnsModel apply(OrderReturns entity) {
				return ShopOrderConvertUtils.getOrderReturns(entity,null);
			}
		});
		return BaseResult.ok(data);
	}
	
	@PostMapping("/applyOrderReturns")
	@SysLog("订单退款退货申请")
	@ApiOperation(value="订单退款退货申请")
	public BaseResult applyOrderReturns(@RequestBody OrderReturnsApplyParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		shopAfterSaleReturnsService.add(params, userInfo);
		return BaseResult.ok();
	}
	
	@GetMapping("/getOrderReturns")
	@SysLog("订单退款退货明细")
	@ApiOperation(value="订单退款退货明细")
	public BaseResult getOrderReturns(@RequestParam("sn")String sn) {
		OrderReturnsModel data=shopAfterSaleReturnsService.get(sn);
		return BaseResult.ok(data);
	}
	
	@GetMapping("/cancelOrderReturns")
	@SysLog("订单退款退货撤销")
	@ApiOperation(value="订单退款退货撤销")
	public BaseResult cancelOrderReturns(@RequestParam("sn")String sn) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		shopAfterSaleReturnsService.cancel(sn, userInfo);
		return BaseResult.ok();
	}
	
	
	@PostMapping("/sendOrderReturns")
	@SysLog("订单退款退货发货")
	@ApiOperation(value="订单退款退货发货")
	public BaseResult sendOrderReturns(@RequestBody OrderReturnsSendParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		shopAfterSaleReturnsService.send(params, userInfo);
		return BaseResult.ok();
	}
	

	
}
